/**
 * 
 */
package com.jm.web.data.form;

import java.util.ArrayList;
import java.util.Collection;

import net.sf.json.JSONArray;

import com.jm.utils.StringHelper;

/**
 * @author yanjie
 *
 * 2011/10/6 上午10:06:12
 */
public class ListItemCollection {

    private static String SATAB = "##SA_TAB##";
    private static String SARET = "##SA_RET##";
    
    protected ArrayList arrItem = new ArrayList();
    public ListItemCollection()
    {
    }

    public void Add(Item item)
    {
        arrItem.add(item);
    }
    
    public void addAll(Collection item)
    {
        arrItem.addAll(item);
    }

    public void Insert(int nPos, Item item)
    {
        arrItem.add(nPos, item);
    }

    public int size()
    {
        return arrItem.size();
    }

    public Item Get(int nIndex)
    {
        return (Item) arrItem.get(nIndex);
    }

    public void Clear()
    {
        arrItem.clear();
    }

    public void Remove(Item item)
    {
        arrItem.remove(item);
    }

    public void Remove(int nIndex)
    {
        arrItem.remove(nIndex);
    }

    public Item FindByValue(String strValue)
    {
        int nItemCount = arrItem.size();
        for (int i = 0; i < nItemCount; i++)
        {
            Item temp = (Item) arrItem.get(i);
            if (strValue.compareTo(temp.getValue()) == 0)
            {
                return temp;
            }
        }
        return null;

    }

    public void CheckAll(boolean bCheck)
    {
        int nItemCount = arrItem.size();
        for (int i = 0; i < nItemCount; i++)
        {
            Item temp = (Item) arrItem.get(i);
            temp.setSelected(bCheck);
        }
    }

    //导出到字符串
    public String toString()
    {
        String strRet = "";
        int nItemCount = arrItem.size();
        for (int i = 0; i < nItemCount; i++)
        {
            Item temp = (Item) arrItem.get(i);
            String strText = temp.getText().replace("\t", SATAB).replace("\n", SARET); ;
            String strValue = temp.getValue().replace("\t", SATAB).replace("\n", SARET); ;
            strRet += strText;
            strRet += "\t";
            strRet += strValue;
            strRet += "\n";
        }
        return strRet;
    }
    
    public void fromString(String strSaveString)
    {
        arrItem.clear();
        if (StringHelper.StringLength(strSaveString) == 0)
        {
            return;
        }

        String[] strPairs = strSaveString.split("\n");
        for (int i = 0; i < strPairs.length; i++)
        {
            String strPair = strPairs[i];
            if (strPair != "")
            {
                String[] strValues = strPair.split("\t");
                if (strValues.length == 2)
                {
                    String strText = strValues[0].replace(SATAB, "\t").replace(SARET, "\n");
                    String strValue = strValues[1].replace(SATAB, "\t").replace(SARET, "\n");
                    this.Add(new Item(strText, strValue));
                }
                else
                {
                    String strText = strValues[0].replace(SATAB, "\t").replace(SARET, "\n");
                    this.Add(new Item(strText, ""));
                }
            }
        }
    }
    
    public String toJsonString(){
    	ArrayList list = new ArrayList();
    	  int nListCount = arrItem.size();
        for (int i = 0; i < nListCount; i++)
        {
            Item tempItem = this.Get(i);
            list.add(tempItem.extAttrList);
        }
        
    	return JSONArray.fromCollection(list).toString();
    }
    
    public String toArrayString(){
    	StringBuffer sb = new StringBuffer();
    	sb.append("[");
	    	ArrayList list = new ArrayList();
	    	  int nListCount = arrItem.size();
	        for (int i = 0; i < nListCount; i++)
	        {
	            Item tempItem = this.Get(i);
	            if(tempItem.extAttrList != null){
	            	
	            }
	        }
        sb.append("]");
    	return sb.toString();
    }

    
}
